package com.cndym.utils;

import com.cndym.exception.CndymException;
import com.cndym.exception.ErrCode;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXValidator;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * 作者：邓玉明 时间：10-12-20 上午10:10  
 */
public class SchemaUtils {
	public static void check(String xml, String xsd) {
		try {
			XMLErrorHandler errorHandler = new XMLErrorHandler();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(true);
			factory.setNamespaceAware(true);
			SAXParser parser = factory.newSAXParser();
			//SAXReader xmlReader = new SAXReader();
			Document xmlDocument = DocumentHelper.parseText(xml);
			parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
			parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + xsd);
			SAXValidator validator = new SAXValidator(parser.getXMLReader());
			validator.setErrorHandler(errorHandler);
			validator.validate(xmlDocument);
			XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
			if (errorHandler.getErrors().hasContent()) {
				writer.write(errorHandler.getErrors());
				throw new IllegalArgumentException(ErrCode.E0002);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			// logger.info("XML文件: " + xml + " 通过XSD文件:" + xsd + "检验失败。\n原因： " + ex.getMessage());
			throw new IllegalArgumentException("schema校验header错误");
		}
	}

	public static void checkBody(String xml, String xsd) {
		try {
			XMLErrorHandler errorHandler = new XMLErrorHandler();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(true);
			factory.setNamespaceAware(true);
			SAXParser parser = factory.newSAXParser();
			//SAXReader xmlReader = new SAXReader();
			Document xmlDocument = DocumentHelper.parseText(xml);
			parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
			parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + xsd);
			SAXValidator validator = new SAXValidator(parser.getXMLReader());
			validator.setErrorHandler(errorHandler);
			validator.validate(xmlDocument);
			XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
			if (errorHandler.getErrors().hasContent()) {
				writer.write(errorHandler.getErrors());
				throw new CndymException(ErrCode.E0002);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			// logger.error("XML文件: " + xml + " 通过XSD文件:" + xsd + "检验失败。\n原因： " + ex.getMessage());
            throw new IllegalArgumentException("schema校验body错误");
		}
	}

    public static void main(String args[]){
        //联盟开奖信息查询
//        checkBody("<body><bonusInfoQuery><issue lottery=\"aA1\" name=\"456\" /></bonusInfoQuery></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\bonusInfoQueryForUnion.xsd");
        //联盟期次查询
//        checkBody("<body><issueQuery><issue lottery=\"aA1\" name=\"456\" /></issueQuery></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\mainIssueQueryForUnion.xsd");
        //联盟反奖查询
//        checkBody("<body><bonusQuery><order id=\"qwe121111111\" /><order id=\"qwe121111112\" /></bonusQuery></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\bonusQueryForUnion.xsd");
        //联盟订单查询
//        checkBody("<body><orderQuery><order outOrderId=\"qwe121111111\" partnersCode=\"asd123123123\" /><order outOrderId=\"qwe121111112\" partnersCode=\"asd123123123\" /></orderQuery></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\programsQueryForUnion.xsd");
        
        //0088  queryUserForMobileAndName
//        checkBody("<body><request><user userCode=\"\" userName=\"cndym\" mobile=\"cndym\" email=\"552211\" cardCode=\"513701198402093750\"/></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\queryUserForMobileAndName.xsd");
        //0087  userSmsAdd
//        checkBody("<body><request><userSms mobile=\"cndym\" verifyCode=\"\" smsBody=\"你好啊\nasdf\"/></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\userSmsAdd.xsd");
        //0086  programsPackageDetailsQuery
//        checkBody("<body><request><query packageId=\"1231111\" /></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\programsPackageDetailsQuery.xsd");
        //0085  programsPackageQuery
//        checkBody("<body><request><query userCode=\"1231111\" lotteryCode=\"\" packageName=\"\" status=\"\" startTime=\"\" endTime=\"\" page=\"\" /></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\programsPackageQuery.xsd");
        //0084  autoSubscribeQuery
//        checkBody("<body><request><query page=\"1231111\" pageSize=\"\" sponsor=\"\" custom=\"\" lotteryCode=\"\" programsOrderId=\"\" startTime=\"\" endTime=\"\" orderStatus=\"\" /></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\autoSubscribeQuery.xsd");
        //0083  forgetPasswordByEmail
//        checkBody("<body><request><user userCode=\"1231111\" email=\"\"  /></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\forgetPasswordByEmail.xsd");
        //0083  forgetPasswordByEmail
        checkBody("<body><request><user userCode=\"1231111\" email=\"\"  /></request></body>","F:\\IDEA\\pros\\lottery\\resources\\schema\\forgetPasswordByEmail.xsd");

        //0082
        //0081
        //0080
        //0079
        //0078
        //0077
        //0076
        //0075
        //0074
        //0073  还没写
        //0072
        //0071
        //0068
        //0067
        //0066
        //0065
        //0064  写了还未验证
        //0063
        //0062
        //0061
        //0060
        //0059
        //0058
        //0057
        //0056
        //0055
        //0054
        //0053
        //0052
        //0051
        //0050  修改的还未验证

        //0016
        //0015
        //0014
        //0013
        //0012
        //0011
        //0010

        //0041
        //0040
        //0039
        //0038
        //0036
        //0035
        //0034
        //0033
        //0032  还没写
        //0031
        //0037
        //0030
        //0009
        //0008
        //0007
        //0006
        //0005
        //0004
        //0003
        //0002  原来就有
        //0001  原来差参数，已改
    }
}
